package com.zygh.interactiveplatform.util;


import com.zygh.interactiveplatform.module.entity.SysStatusData;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.SigarException;

import java.text.DecimalFormat;

/**
 * 监控系统的工具类
 * @author 954
 */
public class SysStatusUtil {

    static {
        try {
            String classPath = SysStatusUtil.class.getClassLoader().getResource("").getPath().concat("sigarPackages");
            String path = System.getProperty("java.library.path");
            String osName = System.getProperty("os.name").toLowerCase();
            path += osName.contains("windows") ? ";".concat(classPath) :
                    osName.contains("linux") ? ":".concat(classPath) : "" ;
            System.setProperty("java.library.path", path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新系统状态配置内容
     */
    public static void updateSysStatus() throws SigarException {
        SysStatusData statusData = new SysStatusData();
        CpuPerc perc = statusData.getPerc();
        // 获取cpu使用率
        String sysCpuUsed = CpuPerc.format(perc.getSys());
        // 获取内存使用率
        Mem mem = statusData.getMem();
        String sysMemoryUsed = new DecimalFormat("0.0%").format(
                Double.valueOf(mem.getUsed()) / Double.valueOf(mem.getTotal()));
        // 更改资源文件
        //PropertiesUtil.setProperty("sysStatus","cpuUsed",sysCpuUsed) ;
       // PropertiesUtil.setProperty("sysStatus","memoryUsed",sysMemoryUsed) ;
    }

}
